import presetVar from '../presetVar';

const { body1} = presetVar.body;
const {head1} = presetVar.head;

/**
 * 表头合计处理
 */
export function headSumHandle() {
    ///需要汇总的金额字段
    let bodyFieldArr = ["assume_amount"];
    let changeHeadItem = {};
    bodyFieldArr.forEach((bodyField) => {//循环字段
        let totalMny = 0;
        let colVal = this.props.cardTable.getColValue(body1, bodyField);//取列值
        let rows = this.props.cardTable.getVisibleRows(body1);
        if (rows) {
            rows.forEach((row) => {
                totalMny += parseFloat(+row.values[bodyField].value);//汇总
            });
        }
        let headField = bodyField == "assume_amount" ? "total" : bodyField;//total字段特殊处理
        let scale = this.props.form.getFormItemsValue(head1, headField).scale;//取精度
        changeHeadItem[headField] = { value: totalMny, display: null };
        if (bodyField == "assume_amount") {
            changeHeadItem['ybje'] = { value: totalMny, display: null };
        }
    });
    this.props.form.setFormItemsValue(head1, changeHeadItem) 
}
/**
 * 增行、插行时，表体相关字段需根据表头设置其编辑性
 * @param {*} props 
 * @param {*} index 
 */
export function AddRow(props,index){
    let rowIndex = props.cardTable.getVisibleRows(body1).length - 1;
    let keys = ['bzbm','bbhl','zzhllx','zzhllyrq','customer','customer_v','hbbm','hbbm_v']
    let default_values = {}
    keys.forEach(key => {
        let headvalue = props.form.getFormItemsValue(head1,key);
        default_values[key] = headvalue
    })
    props.cardTable.setValByKeysAndIndex(body1, rowIndex, default_values)
    let isEditKeys = ['bbhl','zzhllx','zzhllyrq','customer','customer_v','hbbm','hbbm_v'] //表体需要控制的编辑性key
    let editkeys = []
    let unedikeys = []
    isEditKeys.forEach((item) => {
        props.form.getFormItemsDisabled('head', item) ? unedikeys.push(item) : editkeys.push(item)
    })
    props.cardTable.setEditableByIndex(body1, rowIndex, editkeys, true)
    props.cardTable.setEditableByIndex(body1, rowIndex, unedikeys, false)
}
